﻿package com.gengine.global
{
    import __AS3__.vec.*;
    import com.gengine.core.*;
    import com.gengine.core.call.*;
    import com.gengine.core.frame.*;
    import com.gengine.resource.mutiThread.*;
    import com.gengine.ui.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class Global extends Object
    {
        private var _caller:Caller;
        private var _timer:FrameTimer;
        private var _enterSecondDic:Dictionary;
        private var _enterSecondLastAction:Dictionary;
        private var _enterSecondLimits:Dictionary;
        private var _enterFrameVec:Vector.<Function>;
        private var _enterFrameArray:Array;
        private static var _stage:Stage;
        public static var isDebugModle:Boolean = false;
        public static const maxQueue:int = 4;
        private static var _isActivate:Boolean = true;
        private static var _isMinimize:Boolean = false;
        private static const CallerType:String = "render";
        private static var _instance:Global;
        public static var threadManager:IThreadManager;
        public static var application:Application;

        public function Global()
        {
            this._caller = new Caller();
            this._enterSecondDic = new Dictionary();
            this._enterSecondLastAction = new Dictionary();
            this._enterSecondLimits = new Dictionary();
            this._enterFrameVec = new Vector.<Function>;
            this._enterFrameArray = new Array();
            if (_instance != null)
            {
                throw new Error("Global 单例");
            }
            return;
        }// end function

        public function initStage(param1:Stage) : void
        {
            _stage = param1;
            _stage.addEventListener(Event.RESIZE, this.onReSizeHandler, false, 99999, false);
            _stage.addEventListener(Event.ACTIVATE, this.onActivateHandler, false, 99999);
            _stage.addEventListener(Event.DEACTIVATE, this.onDeactivateHandler, false, 99999);
            _stage.addEventListener(ThrottleEvent.THROTTLE, this.onThrottleHandler, false, 99999);
            if (!this._timer)
            {
                this._timer = new FrameTimer(1, int.MAX_VALUE, true);
            }
            this._timer.addListener(TimerType.ENTERFRAME, this.onEnterFrame);
            this._timer.start();
            return;
        }// end function

        private function onEnterFrame(param1:FrameTimer) : void
        {
            var _loc_2:* = undefined;
            var _loc_3:int = 0;
            if (!param1.isRepair)
            {
                for each (_loc_2 in this._enterFrameArray)
                {
                    
                    (_loc_2 as Function).call(null);
                }
                if (this._enterFrameVec.length)
                {
                    _loc_3 = 0;
                    while (_loc_3 < this._enterFrameVec.length)
                    {
                        
                        this._enterFrameVec[_loc_3].call(null);
                        _loc_3++;
                    }
                    this._enterFrameVec.length = 0;
                }
                this.checkAndTakeSecondAction();
            }
            return;
        }// end function

        public function addEnterFrame(param1:Function) : void
        {
            var _loc_2:* = this._enterFrameArray.indexOf(param1);
            if (_loc_2 == -1)
            {
                this._enterFrameArray.push(param1);
            }
            return;
        }// end function

        public function removeEnterFrame(param1:Function) : void
        {
            var _loc_2:* = this._enterFrameArray.indexOf(param1);
            if (_loc_2 >= 0)
            {
                this._enterFrameArray.splice(_loc_2, 1);
            }
            return;
        }// end function

        public function addEnterSecond(param1:Function, param2:int = -1) : void
        {
            this._enterSecondDic[param1] = FrameManager.flashFrame.getTime();
            this._enterSecondLastAction[param1] = 0;
            this._enterSecondLimits[param1] = param2;
            return;
        }// end function

        public function removeEnterSecond(param1:Function) : void
        {
            if (this._enterSecondDic[param1] != null)
            {
                delete this._enterSecondDic[param1];
                delete this._enterSecondLastAction[param1];
                delete this._enterSecondLimits[param1];
            }
            return;
        }// end function

        private function checkAndTakeSecondAction() : void
        {
            var _loc_2:* = undefined;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_1:* = FrameManager.flashFrame.getTime();
            for (_loc_2 in this._enterSecondDic)
            {
                
                _loc_3 = int(this._enterSecondDic[_loc_2]);
                _loc_4 = int(this._enterSecondLastAction[_loc_2]);
                _loc_5 = Math.floor((_loc_1 - _loc_3) / 1000);
                if (_loc_4 == _loc_5)
                {
                    continue;
                }
                this._enterSecondLastAction[_loc_2] = _loc_5;
                (_loc_2 as Function).call();
                _loc_6 = this._enterSecondLimits[_loc_2];
                if (_loc_6 > 0 && _loc_5 >= _loc_6)
                {
                    this.removeEnterSecond(_loc_2 as Function);
                }
            }
            return;
        }// end function

        private function onActivateHandler(event:Event) : void
        {
            MapFrameUtil.start();
            _stage.focus = stage;
            isActivate = true;
            return;
        }// end function

        private function onDeactivateHandler(event:Event) : void
        {
            MapFrameUtil.stop();
            isActivate = false;
            return;
        }// end function

        private function onThrottleHandler(event:ThrottleEvent) : void
        {
            switch(event.type)
            {
                case ThrottleType.THROTTLE:
                {
                    _isMinimize = event.targetFrameRate < 20;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function onReSizeHandler(event:Event) : void
        {
            if (event.target is Stage == false)
            {
                event.stopImmediatePropagation();
            }
            return;
        }// end function

        public function callLater(param1:Function) : void
        {
            if (this._enterFrameVec.indexOf(param1) == -1)
            {
                this._enterFrameVec.push(param1);
            }
            return;
        }// end function

        public function set frameRate(param1:int) : void
        {
            stage.frameRate = param1;
            return;
        }// end function

        public static function get isMinimize() : Boolean
        {
            return _isMinimize;
        }// end function

        public static function set isMinimize(param1:Boolean) : void
        {
            _isMinimize = param1;
            return;
        }// end function

        public static function get isActivate() : Boolean
        {
            return _isActivate;
        }// end function

        public static function set isActivate(param1:Boolean) : void
        {
            _isActivate = param1;
            return;
        }// end function

        public static function get instance() : Global
        {
            if (_instance == null)
            {
                _instance = new Global;
            }
            return _instance;
        }// end function

        public static function get hardwareState() : int
        {
            var _loc_1:* = FrameUtil.driveInfo;
            if (_loc_1 == "")
            {
                return -1;
            }
            if (_loc_1.search(/directx|opengl""directx|opengl/gi) != -1)
            {
                return 1;
            }
            return 0;
        }// end function

        public static function get stage() : Stage
        {
            return _stage;
        }// end function

        public static function gc() : void
        {
            try
            {
                new LocalConnection().connect("foo");
                new LocalConnection().connect("foo");
            }
            catch (e)
            {
            }
            return;
        }// end function

    }
}
